<template>
  <div class="page-container">
    <div class="tool-container">
      <el-form class="tool-search" :model="queryParams" ref="queryFormRef" :inline="true">
        <el-form-item prop="regionCode">
          <RegionSelect v-model="queryParams.regionCode" placeholder="行政区域" clearable />
        </el-form-item>
        <!-- <el-form-item prop="stationId">
          <el-input v-model="queryParams.stationId" placeholder="站房名称" clearable />
        </el-form-item> -->
        <el-form-item prop="stationId">
          <el-select
            v-model="queryParams.stationId"
            filterable
            clearable
            remote
            reserve-keyword
            placeholder="站房名称"
            remote-show-suffix
            :remote-method="queryStationList"
            :loading="loading"
          >
            <el-option v-for="item in stations" :key="item.stationId" :label="item.name" :value="item.stationId" />
          </el-select>
        </el-form-item>

        <el-form-item prop="diviceType">
          <DictSelect dict="station_device_type" v-model="queryParams.diviceType" placeholder="设备类型" clearable />
        </el-form-item>
        <el-form-item prop="deviceNo">
          <el-input v-model="queryParams.searchValue" placeholder="设备编号" clearable />
        </el-form-item>
        <el-form-item prop="status">
          <DictSelect dict="device_status" v-model="queryParams.status" placeholder="状态" clearable />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="Search" @click="handleQuery" v-hasPermi="['biz:device:list']">搜索</el-button>
          <el-button icon="Refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="tabel-container">
      <vxe-grid ref="tableRef" v-bind="gridOptions">
        <template #type="{ row }">
          <dict-tag :options="station_device_type" :value="row.deviceType" />
        </template>
        <template #status="{ row }">
          <dict-tag :options="device_status" :value="row.status" />
        </template>
        <template #mode="{ row }">
          <el-switch :value="row.operationalMode=='1'" disabled> </el-switch>
        </template>

        <template #handle="{ row }">
          <el-button link type="primary" @click="handleDetail(row)" v-hasPermi="['biz:device:query']">详情</el-button>
          <el-button
            link
            type="primary"
            :disabled="row.status == '301' || row.status == '302'"
            @click="handleUpdate(row)"
            v-hasPermi="['biz:device:edit']"
            >编辑</el-button
          >
          <el-button
            link
            type="danger"
            :disabled="row.status == '301' || row.status == '302'"
            @click="handleInvalid(row)"
            v-hasPermi="['biz:device:edit']"
            >作废</el-button
          >
        </template>
        <template #bottom>
          <pagination
            v-show="total > 0"
            :total="total"
            v-model:page="queryParams.pageNum"
            v-model:limit="queryParams.pageSize"
            @pagination="getList"
          />
        </template>
      </vxe-grid>
    </div>

    <el-drawer title="编辑" v-model="showEdit" direction="rtl" class="demo-drawer" :close-on-click-modal="false">
      <Edit ref="edit" @hideRight="showEdit=false;" :data="currentItem" />
    </el-drawer>
  </div>
</template>

<script setup name="StationDevice" lang="ts">
import Edit from './edit/index.vue';
import { abolishDevice, listStationDevice } from '@/api/device/index';
import { listStation } from '@/api/station/index';
import { changeDevice } from '@/api/device/index';
import { DeviceVo } from '@/api/device/types';
import { getTableConfig } from './columns';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { station_device_type, device_status } = toRefs<any>(proxy?.useDict('station_device_type', 'device_status'));
const gridOptions = reactive(getTableConfig());
const data = toRef(gridOptions, 'data');
const stations = ref([]);
const loading = toRef(gridOptions, 'loading');

const total = ref(0);
const showEdit = ref(false);
const currentItem = ref<DeviceVo>();

const queryFormRef = ref<ElFormInstance>();

const queryParams = ref<PageQuery>({
  pageNum: 1,
  pageSize: 20,
  deviceNo: '',
  stationId: '',
  diviceType: '',
  regionCode: '',
  status: ''
});

/**
 * 获取站房列表
 */
const queryStationList = async (name) => {
  loading.value = true;
  const res = await listStation({ name: name });
  loading.value = false;
  if (res.code == 200) {
    stations.value = res.rows;
  }
};

/** 查询租户列表 */
const getList = async () => {
  loading.value = true;
  const res = await listStationDevice(queryParams.value);
  data.value = res.rows;
  total.value = res.total;
  loading.value = false;
};

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};

/** 搜索重置操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
};

/** 更换按钮操作 */
const handleChange = async (row?: DeviceVo) => {
  const _ids = row?.deviceId || '';
  await proxy?.$modal.confirm('是否确认将"' + row?.deviceId + '"标记为更换状态？');
  loading.value = true;
  await changeDevice(_ids).finally(() => (loading.value = false));
  await getList();
  proxy?.$modal.msgSuccess('操作成功');
};

/** 作废按钮操作 */
const handleInvalid = async (row?: DeviceVo) => {
  const _ids = row?.deviceId || '';
  await proxy?.$modal.confirm('是否确认将"' + row?.deviceId + '"标记为作废状态？');
  loading.value = true;
  await abolishDevice(_ids).finally(() => (loading.value = false));
  await getList();
  proxy?.$modal.msgSuccess('操作成功');
};

/** 编辑按钮操作 */
const handleUpdate = async (row?: DeviceVo) => {
  currentItem.value = row;
  showEdit.value = true;
};

/** 详情按钮操作 */
const handleDetail = async (row?: DeviceVo) => {
  proxy?.$tab.openPage('/device/stationDeviceDetail', '', { id: row?.deviceId });
};

onMounted(() => {
  getList();
});
</script>
